<template>
  <div class="detail">
    <header>
      <div class="header-returns" v-show="isShow">
        <div @click="goBack">
          <i class="iconfont icon-fanhui"></i>
        </div>
        <div>
          <i class="iconfont icon-kefu"></i>
        </div>
      </div>
      <div 
      class="header-bar" 
      v-show="!isShow"
      :style="styleOption"
      >
        <div>
          <i class="iconfont icon-fanhui"></i>
        </div>
        <div>
          <span>商品详情</span>
          <span>商品评价</span>
        </div>
        <div>
          <i class="iconfont icon-kefu"></i>
        </div>
      </div>
    </header>
    <section ref="wrapper">
      <div>
        <div class="swiper-main">
          <swiper :option="swiperOption">
            <swiper-slide v-for="(item, index) in swiperList" :key="index">
              <img :src="item.imgUrl" alt="" />
            </swiper-slide>
          </swiper>
          <div class="swiper-pagination"></div>
        </div>
        <div class="goods-name">
          <h1>{{goods.name}}</h1>
          <div>快从宏观代发哪款买啊</div>
        </div>
        <div class="goods-price">
          <div class="oprice">
            <span>￥</span>
            <b>{{goods.price}}</b>
          </div>
          <div class="pprice">
            <span>价格:</span>
            <del>￥{{goods.name}}</del>
          </div>
        </div>
      </div>
    </section>

    <footer>
      <div class="add-cart" @click="addCart">加入购物车</div>
      <div>立即购买</div>
    </footer>
  </div>
</template>

<script>
import { Toast } from 'mint-ui';
import http from '@/common/api/request'
import "swiper/dist/css/swiper.css";
import BetterScroll from "better-scroll";
import { swiper, swiperSlide } from "vue-awesome-swiper";

export default {
  components: {
    swiper,
    swiperSlide,
  },
  created(){
    this.id = this.$route.query.id;
    this.getData(); 
  },
  data() {
    return {
        id:0,
        goods:{},
        styleOption:{},
        BetterScroll: "",
        isShow: true,
        swiperOption: {
        autoplay: 3000,
        speed: 1000,
        pagination: {
          el: ".swiper-pagination",
          type: "fraction",
        },
      },
      swiperList: [
        { imgUrl: "./images/goods1.jpg" },
        { imgUrl: "./images/goods2.jpg" },
        { imgUrl: "./images/goods3.jpg" },
      ],
    };
  },
  activated(){
    //判断当前url id 和之前的id是否一致
    console.log('在detail中',this.$route.query.id,'@@@@',this.id);
    if( this.$route.query.id != this.id){
        this.getData();
        this.id = this.$route.query.id;
    }
  },
  methods:{
    //加入购物车
    addCart(){
      let id = this.$route.query.id;
      http.$axios({
        url:'/api/addCart',
        method:'post',
        // data:{
        //   //原本是id  =》this.id
        //   goodsId:this.id
        // },
        // headers:{
        //   token:true
        // }
      }).then(res=>{
        console.log('在cart中打印res:',res);
        // if( res.success ){
        //   Toast('添加购物车成功')
        // }
      })
    },
    async getData(){
        let res = await http.$axios({
            url:'/api/goods/id',
            params:{
                id:this.$route.query.id
            }
        })
        console.log('在cart中打印1',res);
        this.goods = res
    },
    //返回上一页
    goBack(){
        this.$router.back()
    }
  },
  mounted() {
    this.BetterScroll = new BetterScroll(this.$refs.wrapper, {
      click: true,
      movable: true,
      zoom: true,
      bounce:false,
      probeType:3,
    });
    this.BetterScroll.on('scroll',(pos)=>{
        let posY = Math.abs(pos.y);
        let opacity = posY / 180;
        opacity = opacity > 1 ? 1 : opacity;
        this.styleOption={
                opacity:opacity
            }
        if(posY >= 50 ){
            this.isShow = false;
        }else{
            this.isShow = true;
        }
    })
  },
};
</script>

<style scoped lang="scss">
.detail{
    display: flex;
    flex-direction: column;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}
section{
    flex: 1;
    overflow: hidden;
}
header {
  position: fixed;
  width: 100%;
  top: 0;
  left: 0;
  height: 1.17333rem;
  z-index: 999;
  .header-returns {
    height: 1.17333rem;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      width: 0.9333rem;
      line-height: 0.93333rem;
      margin: 0 0.2666rem;
      background-color: rgba(0, 0, 0, 0.3);
      border-radius: 50%;
      text-align: center;
    }
    i {
      font-size: 0.69333rem;
      color: #fff;
    }
  }
  .header-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 1.173333rem;
    background-clip: #fff;
    font-size: 0.42666rem;
    span {
      padding: 0 0.26666rem;
    }
    i {
      font-size: 0.426666;
    }
  }
}
.swiper-container {
  width: 100%;
  height: 10rem;
}
.swiper-main img {
  width: 100%;
  height: 10rem;
}
.swiper-pagination {
  right: 0.53333rem;
  width: 100%;
  bottom: 0.25555rem;
  text-align: right;
  color: #ffffff;
  font-size: 0.42666rem;
}
.swiper-pagination,
.swiper-pagination-custom,
.swiper-container-horizontal > .swiper-pagination-bullets {
  left: -0.5333rem;
}
.goods-name {
  padding: 0.5333rem 0.266666rem;
  border-bottom: 1px solid #cccc;
  h1 {
    font-size: 0.533333rem;
    font-weight: 500;
  }
  div {
    padding: 0.08rem 0;
    font-size: 0.373333rem;
    color: #999;
  }
}
.goods-price {
  padding: 0.53333rem 0.5333rem;
  .pprice {
    color: #999999;
    span {
      font-size: 0.42rem;
    }
  }
  .oprice {
    color: red;
    font-size: 0.43rem;
  }
}
footer {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1.30666rem;
  border-top: 1px solid #cccccc;
  background-color: #fff;
  margin-bottom: 18px;
  div {
    width: 50%;
    line-height: 1.30666rem;
    text-align: center;
    font-size: 0.4266rem;
    color: #fff;
    background-color: red;
    &.add-cart {
      background-color: #ff9500;
    }
  }
}
</style>